package com.cloudknows.data.qa.zhidao;

import java.io.FileReader;
import java.util.LinkedList;

public class ZhidaoData {
    private LinkedList<Zhidao> zhidaoList = new LinkedList<Zhidao>();
    private final int INSTANCE_NUM_EVERY_TIME = 1000;
    FileReader reader;
    private boolean fileReadUp = false;

    char[] buff = new char[102];
    final String prefix = "__Qid__:";
    final String surfix = "\n__Qid__:";

    private String line = "";
    private int beg = 0;
    private int end = 0;

    public boolean init(String fileName) {
        try {
            reader = new FileReader(fileName);
            int len = 0;

            while (zhidaoList.size() < INSTANCE_NUM_EVERY_TIME && -1 != (len = (reader.read(buff, 0, buff.length)))) {
                line += new String(buff, 0, len);
                while (beg < line.length()) {
                    beg = line.indexOf(prefix, beg);
                    if (-1 == beg)
                        break;
                    end = line.indexOf(surfix, beg + prefix.length());
                    if (-1 == end)
                        break;

                    String zhidaoStr = line.substring(beg, end);
                    Zhidao zhidao = new Zhidao(zhidaoStr);
                    zhidaoList.add(zhidao);
                    beg = end;
                }
                if (-1 == beg)
                    beg = 0;
                line = line.substring(beg);
                beg = 0;
                end = 0;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;

    }

    public Zhidao next() {
        if (zhidaoList.size() == 0) {
            load();
        }
        if (zhidaoList.size() == 0) {
            return null;
        }

        Zhidao zhidao = zhidaoList.pop();
        return zhidao;
    }

    public void load() {
        if (fileReadUp) {
            return;
        }
        try {
            int len = 0;

            while (zhidaoList.size() < INSTANCE_NUM_EVERY_TIME && -1 != (len = (reader.read(buff, 0, buff.length)))) {
                line += new String(buff, 0, len);
                while (beg < line.length()) {
                    beg = line.indexOf(prefix, beg);
                    if (-1 == beg)
                        break;
                    end = line.indexOf(surfix, beg + prefix.length());
                    if (-1 == end)
                        break;

                    String zhidaoStr = line.substring(beg, end);
                    Zhidao zhidao = new Zhidao(zhidaoStr);
                    zhidaoList.add(zhidao);
                    beg = end;
                }
                if (-1 == beg)
                    beg = 0;
                line = line.substring(beg);
                beg = 0;
                end = 0;

            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (zhidaoList.size() < INSTANCE_NUM_EVERY_TIME) {
            zhidaoList.add(new Zhidao(line));
            fileReadUp = true;
        }
    }

    public static void main(String[] args) throws Exception {
        String fileName = "../cloud-knows/data/zhidao/zhidao_sample.txt";
        ZhidaoData zhidaoData = new ZhidaoData();
        zhidaoData.init(fileName);
        Zhidao zhidao = null;
        while (null != (zhidao = zhidaoData.next())) {
            System.out.println("ID:" + zhidao.getId());
            System.out.println("FIELD:" + zhidao.getField());
            System.out.println("Q:" + zhidao.getQuestion());
            System.out.println("A:" + zhidao.getAnswers());
        }
    }

}

